package com.example.mainstream.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.myweb.domain.User;
import com.alibaba.fastjson.JSONObject;
import com.myapp.activity.GoodsDetailActivity.GoodsDetailThread;
import com.myapp.activity.GoodsDetailActivity.PutInCarThread;
 
import com.myapp.adapter.GoodsCarListAdapter;
 
import com.myapp.common.HttpUtil;
import com.myweb.domain.GoodsCar;
 
import com.myweb.domain.Note;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class GoodsCarActivity extends ActionBarActivity {

	TextView goodscar_sumpricetv;

	Button goodscar_paybtn;

	Button goodscar_clearbtn;

	ListView listView;

	List<Map<String, Object>> goodscarlist1;

	private GoodsCarListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_goods_car);

		listView = (ListView) findViewById(R.id.goodscar_list);// ��ȡ�б�Ԫ��

		goodscarlist1 = new ArrayList<Map<String, Object>>();

		adapter = new GoodsCarListAdapter(this, goodscarlist1);// ������

		listView.setAdapter(adapter);

		goodscar_sumpricetv = (TextView) findViewById(R.id.goodscar_sumpricetv);

		goodscar_paybtn = (Button) findViewById(R.id.goodscar_paybtn);
		
		goodscar_paybtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
							
				
				Float sumprice=Float.valueOf(goodscar_sumpricetv.getText().toString());
				
                if(!(sumprice>0))
                {
                	Toast.makeText(getApplicationContext(), "û��Ҫ�������Ʒ!",
    						Toast.LENGTH_SHORT).show();
                }
                else
                {
                	
                	Intent intent = new Intent(GoodsCarActivity.this,
    						OrderCreateActivity.class);
    				
    				intent.putExtra("sumprice", goodscar_sumpricetv.getText());
    				
    				// ����Activity
    				startActivity(intent);
                }			
			}
		});

		goodscar_clearbtn = (Button) findViewById(R.id.goodscar_clearbtn);

		goodscar_clearbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

				Thread clearThread = new Thread(new ClearCarThread());

				clearThread.start();
			}
		});

		Thread goodsCarListThread = new Thread(new GoodsCarListThread());

		goodsCarListThread.start();

	}

	public void DeleteCarItem(String id) {

		Thread deleteCarItemThread = new Thread(new DeleteCarItemThread(id));

		deleteCarItemThread.start();
	}

	class DeleteCarItemThread implements Runnable {

		private String _carid = "";

		public DeleteCarItemThread(String carid) {

			this._carid = carid;
		}

		public void run() {
			try {

				JSONObject paraObj = new JSONObject();

				paraObj.put("action", "delete");

				paraObj.put("goodscarid", _carid);

				String result = HttpUtil.getJsonFromServlet(paraObj.toString(),
						"GoodsCarService");

				// String result ="ok";

				if (result.equals("ok")) {

					Message msg = new Message();

					msg.what = 1;// �ɹ�

					handler.sendMessage(msg);
				}

			} catch (Exception ex) {

				System.out.println("ex: " + ex.getMessage());

				Message msg = new Message();

				msg.what = 2;// �����쳣

				handler.sendMessage(msg);

			}
		}
	}

	class GoodsCarListThread implements Runnable {

		public void run() {
			try {

				JSONObject paraObj = new JSONObject();

				paraObj.put("action", "list");

				MyApplication myApp = (MyApplication) getApplication(); // ����Զ����Ӧ�ó���MyApp

				paraObj.put("userid", String.valueOf(myApp.getUser().getId()));

				String result = HttpUtil.getJsonFromServlet(paraObj.toString(),
						"GoodsCarService");

				if (!result.equals("")) {

					List<GoodsCar> goodscarList = JSONObject.parseArray(result,
							GoodsCar.class);

					ArrayList<Map<String, Object>> goodscarListMap = new ArrayList<Map<String, Object>>();

					for (GoodsCar goodscar : goodscarList) {

						Map<String, Object> map = new HashMap<String, Object>();

						map.put("id", goodscar.getId());

						String _name = goodscar.getGoodsname();

						if (_name.length() > 16) {
							_name = _name.substring(0, 16) + "...";
						}
						map.put("name", _name);

						map.put("buynum", goodscar.getBuynum());

						if (goodscar.getImgpath().length() > 0) {
							
							map.put("imgpath",
									HttpUtil.BASE_URL + goodscar.getImgpath());
						}
						else
						{
							map.put("imgpath","");
						}

						map.put("sumprice", goodscar.getSumprice());

						goodscarListMap.add(map);
					}

					Message msg = new Message();

					msg.what = 0;// ���ʳɹ����з���ֵ

					msg.obj = goodscarListMap;

					handler.sendMessage(msg);

				}

			} catch (Exception ex) {

				System.out.println("ex: " + ex.getMessage());

				Message msg = new Message();

				msg.what = 2;// �����쳣

				handler.sendMessage(msg);

			}
		}
	}

	// ��չ��ﳵ
	class ClearCarThread implements Runnable {

		public void run() {

			try {

				JSONObject paraObj = new JSONObject();

				paraObj.put("action", "clear");

				MyApplication myApp = (MyApplication) getApplication(); // ����Զ����Ӧ�ó���MyApp

				paraObj.put("userid", String.valueOf(myApp.getUser().getId()));

				String result = HttpUtil.getJsonFromServlet(paraObj.toString(),
						"GoodsCarService");

				if (result.equals("ok")) {

					Message msg = new Message();

					msg.what = 1;// �ɹ�

					handler.sendMessage(msg);
				}

			} catch (Exception ex) {

				System.out.println("ex: " + ex.getMessage());

				Message msg = new Message();

				msg.what = 2;// �����쳣

				handler.sendMessage(msg);

			}
		}
	}

	// Handler
	Handler handler = new Handler() {

		public void handleMessage(Message msg) {

			super.handleMessage(msg);

			switch (msg.what) {

			case 0:
				try {
					ArrayList<Map<String, Object>> result = (ArrayList<Map<String, Object>>) msg.obj;

					if (result != null) {

						float sumprice = 0;

						for (Map<String, Object> map : result) {
							sumprice += Float.parseFloat(map.get("sumprice")
									.toString());
						}

						goodscar_sumpricetv.setText(String.valueOf(sumprice));

						goodscarlist1.clear();

						goodscarlist1.addAll(result);

						adapter.notifyDataSetChanged();

						System.out.println(result.toString());
					}
				} catch (Exception ex) {

					System.out.println("ex: " + ex.getMessage());
				}
				break;
			case 1:

				Thread goodsCarListThread = new Thread(new GoodsCarListThread());

				goodsCarListThread.start();

				Toast.makeText(getApplicationContext(), "�����ɹ�!",
						Toast.LENGTH_SHORT).show();
				break;

			case 2:

				Toast.makeText(getApplicationContext(), "����ʧ�ܣ���������!",
						Toast.LENGTH_SHORT).show();
				break;
			}

		}
	};
	

	// ��ҳ��ر�ʱ����
	@Override
	protected void onResume() {

		super.onResume();

		handler1.post(runnable);
	}

	private Runnable runnable = new Runnable() {

		public void run() {
			// ������
			handler1.sendEmptyMessage(1);
		}
	};

	private Handler handler1 = new Handler() {

		public void handleMessage(Message msg) {

			switch (msg.what) {

			case 1:

				Thread goodsCarListThread = new Thread(new GoodsCarListThread());

				goodsCarListThread.start();

				
				break;
			}
		};
	};


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.add_question, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	class ClickEvent implements OnClickListener {

		@Override
		public void onClick(View v) {
			v.getTag();
		}

	}

}
